package com.mvp.shq.washer.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.mvp.shq.washer.R;
import com.mvp.shq.washer.entity.shop.Product;
import com.mvp.shq.washer.utils.DataDictionary;
import com.mvp.shq.washer.utils.ImageUtil;
import com.mvp.shq.washer.utils.StringUtils;
import com.orhanobut.logger.Logger;

import java.math.BigDecimal;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.http.POST;

/**
 * Created by westing on 2017/10/10.
 * 购物车列表
 */

public class ShopcarlistAdapter extends RecyclerView.Adapter<ShopcarlistAdapter.ViewHolder> {

    private Context context;
    private List<Product> datas;
    private ImageUtil imageUtil;
    private CheckLiserent checkLiserent;

    public List<Product> getDatas() {
        return datas;
    }

    public void setDatas(List<Product> datas) {
        this.datas = datas;
    }

    public ShopcarlistAdapter(Context mContext, List<Product> list, CheckLiserent checkLiserent) {
        context = mContext;
        datas = list;
        imageUtil = new ImageUtil(context);
        this.checkLiserent=checkLiserent;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_shopcarlist, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        Product product = datas.get(position);
        String money1 = "",money2 = "",integral = "";
        if (product.getPayMode().equals(DataDictionary.PAY_MODE_CASH_AND_INTEGRAL)){
            money1 = (int)product.getCashPrice()*product.getByNum()+"";
            money2 = StringUtils.BigDecimalToStr(BigDecimal.valueOf(product.getCashPrice()*product.getByNum()));
            integral = (int)product.getIntegralPrice()*product.getByNum()+"";
        }else if (product.getPayMode().equals(DataDictionary.PAY_MODE_CASH)){
            money1 = (int)product.getCashPrice()*product.getByNum()+"";
            money2 = StringUtils.BigDecimalToStr(BigDecimal.valueOf(product.getCashPrice()*product.getByNum()));
        }else if (product.getPayMode().equals(DataDictionary.PAY_MODE_INTEGRAL)){
            integral = (int)product.getIntegralPrice()*product.getByNum()+"";
        }

        if(product.getIntegralPrice()==0){
            holder.llMoney.setVisibility(View.VISIBLE);
            holder.tvAdd.setVisibility(View.GONE);
            holder.llIntegral.setVisibility(View.INVISIBLE);
            holder.tvIntegralPrompt.setVisibility(View.GONE);
            holder.tvMoney1.setText(money1);
            holder.tvMoney2.setText(money2);
        }else if(product.getCashPrice()==0 && product.getIntegralPrice()!=0){
            holder.llMoney.setVisibility(View.GONE);
            holder.tvAdd.setVisibility(View.GONE);
            holder.llIntegral.setVisibility(View.VISIBLE);
            holder.tvIntegralPrompt.setVisibility(View.VISIBLE);
            holder.tvIntegral.setText(integral);
        }else{
            holder.llMoney.setVisibility(View.VISIBLE);
            holder.tvAdd.setVisibility(View.VISIBLE);
            holder.llIntegral.setVisibility(View.VISIBLE);
            holder.tvIntegralPrompt.setVisibility(View.GONE);
            holder.tvMoney1.setText(money1);
            holder.tvMoney2.setText(money2);
            holder.tvIntegral.setText(integral);
        }
        String imgUrl = DataDictionary.SHOP_IMG_BASE_URL+product.getImgUrl().replace(DataDictionary.SHOP_IMG_BASE_URL,"");
        imageUtil.loadUrlImage(imgUrl,holder.imgProductPic);

        holder.tvProductTitle.setText(product.getProductName());
        holder.tvProductType.setText(product.getSpecText());
        holder.tvByNum.setText("x"+product.getByNum());

        holder.tvByType.setVisibility(View.GONE);
        holder.tvBySize.setVisibility(View.GONE);
//        if(checkAll){
//            holder.radiochange.setBackgroundResource(R.mipmap.cart_selected);//选择
//        }else{
//
//        }
        if(product.getIsCheck()){
            holder.radiochange.setBackgroundResource(R.mipmap.cart_selected);
        }else{
            holder.radiochange.setBackgroundResource(R.mipmap.cart_pressed);
        }
        holder.radiochange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //product.setCheck(!product.isCheck());
//                if(product.isCheck()){
//                    holder.radiochange.setBackgroundResource(R.mipmap.cart_selected);
//                    checkLiserent.onCheckItem(position);
//                }else{
//                    holder.radiochange.setBackgroundResource(R.mipmap.cart_pressed);
//                    checkLiserent.onCheckNoItem(position);
//                }
                checkLiserent.onCheckItem(position);
            }
        });

        holder.imgProductPic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkLiserent.itemClick(position);
            }
        });

    }
    private boolean checkAll;

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public boolean isCheckAll() {
        return checkAll;
    }

    public void setCheckAll(boolean checkAll) {
        this.checkAll = checkAll;
        for (int i=0,len=datas.size();i<len;i++){
            datas.get(i).setCheck(checkAll);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return datas!=null?datas.size():0;
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.radiochange)
        ImageView radiochange;
        @BindView(R.id.img_product_pic)
        ImageView imgProductPic;
        @BindView(R.id.tv_product_title)
        TextView tvProductTitle;
        @BindView(R.id.tv_product_type)
        TextView tvProductType;
        @BindView(R.id.tv_by_type)
        TextView tvByType;
        @BindView(R.id.tv_by_size)
        TextView tvBySize;
        @BindView(R.id.tv_money_1)
        TextView tvMoney1;
        @BindView(R.id.tv_money2)
        TextView tvMoney2;
        @BindView(R.id.ll_money)
        LinearLayout llMoney;
        @BindView(R.id.tv_add)
        TextView tvAdd;
        @BindView(R.id.tv_integral)
        TextView tvIntegral;
        @BindView(R.id.ll_integral)
        LinearLayout llIntegral;
        @BindView(R.id.tv_integral_prompt)
        TextView tvIntegralPrompt;
        @BindView(R.id.tv_by_num)
        TextView tvByNum;
        @BindView(R.id.item_product)
        LinearLayout itemProduct;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
    public interface CheckLiserent{
        void itemClick(int position);
        //选中
        void onCheckItem(int position);
    }
}
